const { EventEmitter } = require('events');

export const MediaStatusEvent = {
	publish: 'publishMediaStatus',
	render: 'renderMediaStatus',
};

class MediaStatus extends EventEmitter {
	constructor() {
		super({ captureRejections: true });
        this.publishMediaStatus = [];
		// {
		// 	streamLabel: '', // 本地推流标签
		// 	status: '', // 'failed'|'connecting'|'connected'|'disconnected'|'quality-report'
		//     userInfo: {} // client信息
		// }, // 本地推送画面的媒体连接状态
		this.renderMediaStatus = [];
		// {
		// 	streamLabel: '', // 渲染的流标签
		// 	status: '', // 'failed'|'connecting'|'connected'|'disconnected'|'quality-report'
		//     userInfo: {} // client信息
		// }, // 远端画面的媒体连接状态
	}

	setPublishMediaStatus(data: any) {
        if (!data) return;
        let updated = false;
        const { streamLabel, publishStatus } = data;
		let cloneStatus: any[] = [...this.publishMediaStatus];
		const items = this.publishMediaStatus.filter((item: any) => item.streamLabel === streamLabel);
        if (items && items.length > 0) {
            cloneStatus.map(item => {
                if (item.streamLabel === streamLabel) {
                    const notGood = publishStatus.status === 'quality-report' ? publishStatus.data.status !== 'good' : false;
                    if (publishStatus.status === 'quality-report')  {
                        if (item.notGood !== notGood) {
                            item.notGood = notGood;
                            updated = true;
                            console.warn('publish 1', item);
                        }
                        
                    } else if (item.status !== publishStatus.status) {
                        item.status = publishStatus.status;
                        updated = true;
                        console.warn('publish 2', item);
                    }

                }
            });
        } else {
            let status: any = { streamLabel, status: publishStatus.status, notGood: publishStatus.status === 'quality-report' ? publishStatus.data.status !== 'good' : false};
            cloneStatus.push(status);
            updated = true;
            console.warn('publish 3', status);
        }
        this.publishMediaStatus = cloneStatus;
        if (updated) {
            this.emit(MediaStatusEvent['publish'], this.publishMediaStatus);
        }
	}

    setRenderMediaStatus(data: any) {
        if (!data) return;
        let updated = false;
        const { streamLabel, renderStatus } = data;
		let cloneStatus: any[] = [...this.renderMediaStatus];
		const items = this.renderMediaStatus.filter((item: any) => item.streamLabel === streamLabel);
        if (items && items.length > 0) {
            cloneStatus.map(item => {
                if (item.streamLabel === streamLabel) {
                    const notGood = renderStatus.status === 'quality-report' ? renderStatus.data.status !== 'good' : false;
                    if (renderStatus.status === 'quality-report')  {
                        if (item.notGood !== notGood) {
                            item.notGood = notGood;
                            updated = true;
                        }
                        
                    } else if (item.status !== renderStatus.status) {
                        item.status = renderStatus.status;
                        updated = true;
                    }

                }
            });
        } else {
            let status: any = { streamLabel, status: renderStatus.status, notGood: renderStatus.status === 'quality-report' ? renderStatus.data.status !== 'good' : false};
            cloneStatus.push(status);
            updated = true;
        }
        this.renderMediaStatus = cloneStatus;
        if (updated) {
            console.warn('render ', cloneStatus);
            this.emit(MediaStatusEvent['render'], this.renderMediaStatus);
        }
	}

	/**
	 * 添加媒体状态事件
	 * @param {function} ack 收到回调消息
	 */
	addPublishMediaStatusEventLinstener(ack: any) {
		this.on(MediaStatusEvent['publish'], ack);
	}

    /**
	 * 添加媒体状态事件
	 * @param {function} ack 收到回调消息
	 */
	addRenderMediaStatusEventLinstener(ack: any) {
		this.on(MediaStatusEvent['render'], ack);
	}

	/**
	 * 移除媒体状态事件
	 * @param {function} ack 收到回调消息
	 */
	removePublishMediaStatusEventLinstener() {
		this.removeAllListeners(MediaStatusEvent['publish']);
	}

    /**
	 * 移除媒体状态事件
	 * @param {function} ack 收到回调消息
	 */
	removeRenderMediaStatusEventLinstener() {
		this.removeAllListeners(MediaStatusEvent['render']);
	}

    /**
	 * 清楚本地数据
	 * @param {function} ack 收到回调消息
	 */
    clearMediagData() {
        this.publishMediaStatus = [];
		this.renderMediaStatus = [];
    }
}

export default new MediaStatus();
